<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\ProjectLogic;
use app\cms\model\Project;
use app\cms\model\ProjectMeeting;
use app\cms\validate\ProjectValidate;
use plugin\saiadmin\app\logic\system\SystemUserLogic;
use support\Log;
use support\Request;
use support\Response;
use app\cms\model\project_meeting_invitation\ProjectMeetingInvitationSpeaker;
use app\cms\model\Users;
use app\cms\validate\UsersValidate;
use app\model\DownloadZip;
use support\customHelpers;
use support\PackZip;
use think\facade\Db;
use think\Validate;
use Webman\RedisQueue\Client;
use Webman\RedisQueue\Redis;

/**
 * 
 * 功能批注：
 *1、项目-会议-邀请讲者（线上/线下）-沟通阶段（线上/线下）

 *2、项目完结时检查：会议管理不能有“进行中”状态、邀请阶段不能有“待确认”状态

 *沟通阶段每个讲者资料的必填项全部填写完成

 *3、勾选批量复选框后显示【批量删除】【导出Excel】【下载文件】：涉及项目-会议-讲者-沟通阶段，信息联动导出涉及项目-会议-讲者-沟通阶段，信息联动导出

 *4、【下载文件】：有关文件存储，外讲库上传的文件要按照 项目>>会议>>讲者 这几级自动创建相应文件夹后进行存储，文件夹名称就用各阶段的名称，项目名称、会议名称、讲者姓名

 *5、邀请阶段状态没有待确认，沟通阶段没有空的
 */
/**
 * 项目管理控制器
 */
class ProjectController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new ProjectLogic();
        $this->validate = new ProjectValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        // $where = $request->more([
        //     ['project_name', ''],
        //     ['project_code', ''],
        //     ['order_sn', ''],
        //     ['meeting_type', ''],
        //     ['company_name', ''],
        //     ['project_status', ''],
        // ]);
        $where = $request->more([
            ['project_name', ''],
            ['project_status', ''],
        ]);
        $query = $this->logic->search($where);
        // Log::info('adminInfo:' . json_encode($this->adminInfo));
        $query->with([
            'users' => function ($query) {
                $query->field('realname,phone,status')->where('status', 1);
            },
            'adminUser' => function ($query) {
                $query->field('id,username,nickname')->where('status', 1);
            },
            // 'usersOne' => function ($query) {
            //     $query->field('id,realname,phone,status')->where('status', 1);
            // },
        ]);

        if ($this->adminId !== 1) {

            // $query->where('created_by', $this->adminId) //当前用户创建的
            //     // ->whereOr("FIND_IN_SET(" . $this->adminId . ", collaborator_by)"); //或者分配给当前用户的
            //     ->whereOr("FIND_IN_SET(" . $this->adminId . ", collaborator_by)"); //或者分配给当前用户的
            $query->whereRaw('created_by=:id or FIND_IN_SET(' . $this->adminId . ',collaborator_by)', ['id' => $this->adminId]); //当前用户创建的

        }

        if (!empty($where['project_name'])) {
            $query->whereOr('project_code', 'like', '%' . $where['project_name'] . '%')
                ->whereOr('order_sn', 'like', '%' . $where['project_name'] . '%')
                ->whereOr('service', 'like', '%' . $where['project_name'] . '%')
                //->whereOr('order_amount', 'like', '%' . $where['name'] . '%')
                ->whereOr('company_name', 'like', '%' . $where['project_name'] . '%')
                ->whereOr('student_association', 'like', '%' . $where['project_name'] . '%')
                ->whereOr('foundation', 'like', '%' . $where['project_name'] . '%')
                ->whereOr('third_party', 'like', '%' . $where['project_name'] . '%');
        }

        $data = $this->logic->getList($query);

        return $this->success($data);
    }

    /**
     * 获取分配协作人列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-19
     * @return void
     */
    public function getUserListByAdmin(Request $request)
    {
        $id = $request->input('id', '');
        $systemUser = new SystemUserLogic();
        // @todo 后期修改后台用户筛选条件
        if (!empty($id)) {
            $data = $systemUser->where('status', 1)->where('id', '<>', $id)->field('id, username,nickname')->select();
        } else {
            $data = $systemUser->where('status', 1)->field('id, username,nickname')->select();
        }

        return $this->success($data);
    }

    /**
     * 项目协作
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-16
     * @return void
     */
    public function projectCollaboration(Request $request)
    {
        $id = $request->input('id', '');
        $collaborator_by = $request->input('collaborator_by', '');
        $project = $this->logic->where('id', $id)->update([
            'collaborator_by' => $collaborator_by
        ]);
        if ($project) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
    /**
     * 项目完结
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-16
     * @return void
     */
    public function projectEnd(Request $request)
    {
        $id = $request->input('id', '');
        $project_status = $request->input('project_status', '');
        $remarks = $request->input('remarks', '');
        if ($project_status == 2) {
            // @todo 判断项目下会议管理不能有“进行中”状态、
            // @todo 邀请阶段不能有“待确认”状态
            // @todo 判断沟通记录内，对应项目的常规邮件&发送状态是否完成
            $projectMeeting = ProjectMeeting::where('project_id', $id)->where('status', 1)->count();

            if ($projectMeeting > 0) {
                return $this->fail('项目中有还在进行中的会议');
            }

            // $projectMeetingInvitation = ProjectMeetingInvitationSpeaker::where('project_id', $id)->where('invitation_status', 0)->count();

            // if ($projectMeetingInvitation > 0) {
            //     return $this->fail('邀请阶段还有待确认专家');
            // }
        }
        $project = $this->logic->where('id', $id)->update([
            'project_status' => $project_status,
            'stop_remarks' => $remarks,
            'stop_time' => $project_status == 3 ? date('Y-m-d H:i:s', time()) : null,
            'end_time' => $project_status == 2 ? date('Y-m-d H:i:s', time()) : null,
        ]);
        if ($project) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 项目中止
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-16
     * @return void
     */
    public function projectSuspension(Request $request)
    {
        $id = $request->input('id', '');
        $project_status = $request->input('project_status', '');
        $remarks = $request->input('remarks', '');
        if ($project_status == 2) {
            // @todo 判断项目下会议管理不能有“进行中”状态、
            // @todo 邀请阶段不能有“待确认”状态
            // @todo 判断沟通记录内，对应项目的常规邮件&发送状态是否完成
            $projectMeeting = ProjectMeeting::where('project_id', $id)->where('status', 1)->count();

            $projectMeetingInvitation = ProjectMeetingInvitationSpeaker::where('project_id', $id)->where('invitation_status', 0)->count();

            if ($projectMeeting > 0 || $projectMeetingInvitation > 0) {
                return $this->fail('项目下有还在进行中的会议，或邀请阶段还有待确认专家');
            }
        }
        $project = $this->logic->where('id', $id)->update([
            'project_status' => $project_status,
            'stop_remarks' => $remarks,
            'stop_time' => $project_status == 3 ? date('Y-m-d H:i:s', time()) : null,
            'end_time' => $project_status == 2 ? date('Y-m-d H:i:s', time()) : null,
        ]);
        if ($project) {
            $this->suspension($id, $remarks);
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    // 中止的时候，中止项目下所有会议
    public function suspension($id, $stop_remarks)
    {
        // 中止项目下所有会议、邀请
        ProjectMeeting::where('project_id', $id)->update([
            'status' => -1,
            'stop_remarks' => $stop_remarks,
            'stop_time' => date('Y-m-d H:i:s', time())
        ]);
    }

    /**
     * 项目删除
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-16
     * @return void
     */
    public function projectDelete()
    { }

    /**
     * 是否前端展示
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-13
     * @param Request $request
     * @return void
     */
    public function isDisplay(Request $request)
    {
        $id = $request->input('id', '');
        $status = $request->input('is_display', '');
        $user = $this->logic->where('id', $id)->update([
            'is_display' => $status
        ]);
        if ($user) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }


    /**
     * 是否推荐
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-20
     * @param Request $request
     * @return boolean
     */
    public function isRecommend(Request $request)
    {
        $id = $request->input('id', '');
        $status = $request->input('recommend', '');
        $result = $this->logic->where('id', $id)->update([
            'recommend' => $status
        ]);
        if ($result) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    // @todo 删除项目 - 需要重构、判断项目下 会议管理不能有“进行中”状态、邀请阶段不能有“待确认”状态
    // @todo 项目完结 - 会议管理不能有“进行中”状态、邀请阶段不能有“待确认”状态

    /**
     * 申请下载文件
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-05
     * @param Request $request
     * @return void
     */
    public function downloadZip(Request $request)
    {
        $projectId = $request->input('project_id');
        // $projectMeetingId = $request->input('project_meeting_id');
        // $speakerId = $request->input('speaker_id');
        // $type = $request->input('type');

        // $data = [];
        // switch ($type) {
        //     case 1:
        $data = [
            'project_id' => $projectId,
            'type' => 1,
        ];
        //         break;
        //     case 2:
        //         $data = [
        //             'project_id' => $projectId,
        //             'project_meeting_id' => $projectMeetingId,
        //             'type' => 2,
        //         ];
        //         break;
        //     case 3:
        //         $data = [
        //             'project_id' => $projectId,
        //             'project_meeting_id' => $projectMeetingId,
        //             'project_meeting_invitation_id' => $speakerId,
        //             'type' => 3,
        //         ];
        //         break;
        // }
        $result = DownloadZip::create($data);

        if ($result) {
            // 队列名
            $queue = 'download-zip';

            $projectMeeting = Project::with(
                [
                    // 项目 一对多 项目会议
                    'projectMeeting' => function ($query) {

                        $query->with([
                            // 项目会议 一对多 项目会议邀请讲者
                            'projectMeetingInvitationSpeaker' => function ($query) {
                                $query->field(
                                    '
                                    id,
                                    project_id,
                                    project_meeting_id,
                                    speaker_id,
                                    speaker_name,
                                    speaker_email,
                                    meeting_type,
                                    meeting_remarks,
                                    invitation_status
                                    '
                                )->with(array_merge([
                                    // 关联资料上传表
                                    'uploadData' => function ($query) {
                                        $query->field(' id,
                                                        invitation_speaker_id,
                                                        meeting_time_bj,
                                                        meeting_time_en,
                                                        meeting_start_time,
                                                        meeting_end_time,
                                                        service_charge,
                                                        payer,
                                                        meeting_info_is_display,
                                                        meeting_materiel,
                                                        meeting_screen_recording,
                                                        meeting_photo,
                                                        meeting_materiel_is_display,
                                                        meeting_ppt,
                                                        meeting_ppt_is_display,
                                                        currency_type');
                                    },
                                    // cuc账户录入
                                    // 'cuc' => function ($query) {
                                    //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                    // },
                                    // 劳务费支付
                                    // 'charge' => function ($query) {
                                    //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                    // },
                                    // @todo  如果外讲库的讲者简历、护照、付款信息有的话用那俩表的eb_speaker_passport、eb_speaker_resume
                                    // 索要讲者简历+照片
                                    'speakerResume' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,speaker_resume,speaker_resume_translate,speaker_avatar');
                                    },
                                    // @todo  更新 eb_speaker_resume表付款信息
                                    // 索要讲者付款信息+护照 
                                    'speakerPassport' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,remittance,surname,name,birth,passport_number,nationality,sex,place_of_birth,passport_issuing_place,validity,upload_passport');
                                    },
                                    // 签订协议
                                    'signAgreement' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,sign_agreement_url');
                                    },
                                    // PPT要求及返回截止日期
                                    'pptReturnDeadline' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_return_deadline');
                                    },
                                    // PPT跟进
                                    'pptFollowUp' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_follow_up');
                                    },
                                    // 翻译PPT给到讲者
                                    'pptTranslate' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate');
                                    },
                                    // 翻译讨论问题给讲者
                                    'translateToLecturer' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate_problem');
                                    },
                                    'speaker' => function ($query) {
                                        $query->with([
                                            'resume',
                                            'passport'
                                        ])
                                            ->field('id,institution,institution_en')->where('status', 1);
                                    },

                                ], [
                                    // 线上会议信息+测试需求给到讲者
                                    'meetingOnline' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,conference_number,password,conference_url,conference_is_display,is_test,test_time,test_is_display,is_display');
                                    },
                                    // 可编辑日程翻译给讲者
                                    'meetingSchedule' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,schedule_url');
                                    },
                                    // 会后感谢信
                                    // 'meetingAfter' => function ($query) {
                                    //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                    // },
                                ], [
                                    // 签证计划
                                    // 'visaProgram' => function ($query) {
                                    //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                    // },
                                    // 签证包发送
                                    'visaPackage' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,visa_url');
                                    },
                                    // 邀请函
                                    'invitationLetter' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_url');
                                    },
                                    // 邀请函邮寄发单号
                                    // 'invitationLetterNumber' => function ($query) {
                                    //     $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_number');
                                    // },
                                    // @todo  更新外讲表  eb_speaker_resume  的常旅客号
                                    // 索要常旅客号
                                    // 'frequentFlyer' => function ($query) {
                                    //     $query->field('id,invitation_speaker_id,status,remarks,is_display,frequent_flyer_number');
                                    // },
                                    // 发讲者喜好问卷
                                    'meetingQuestions' =>  function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,questionnaire_url');
                                    },
                                    // 行程终版+国际机票+酒店信息给讲者
                                    'finalTravel' =>  function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,final_travel,ticket,hotel');
                                    },
                                    // 索要中国签证扫描页
                                    'chinaVsiaScan'  => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,scan_url');
                                    },
                                    // 接机安排和温馨提示等
                                    // 'pickUpAndTips'  => function ($query) {
                                    //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                    // },
                                    // 活动感谢信+询问报销+出入境章扫描件
                                    'scan'  => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,thank_you,reimbursement,scan');
                                    },
                                    // 索要报销单据扫描件
                                    'reimbursement'  => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,reimbursement_url');
                                    },
                                    // 发结算单
                                    'statement' => function ($query) {
                                        $query->field('id,invitation_speaker_id,status,remarks,is_display,final_statement_url');
                                    },
                                ]));
                            }
                        ])->field('id,project_id,meeting_name,status'); //->where('status', 2);
                    }
                ]
            )
                ->where('id', $projectId)
                ->field('id,project_name')
                ->find()->toArray();

            if (!empty($projectMeeting['projectMeeting'])) {

                // 投递消息
                Client::send($queue, $projectMeeting);

                return $this->success([], '文件正在生成中···');
            } else {
                return $this->fail('项目中会议数据未完善');
            }
        }
    }


    /**
     * 项目列表导出excel
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-10
     * @param Request $request
     * @return void
     */
    public function projectExport(Request $request)
    {

        $projectId = $request->input('project_id');


        $projectMeeting = Project::with(
            [
                'adminUser' => function ($query) {
                    $query->field('id,username,nickname,phone')->where('status', 1);
                },
                'usersOne' => function ($query) {
                    $query->field('id,username,realname,avatar,email,company_name,phone,last_login_time');
                },
                'users',
                // 项目 一对多 项目会议
                'projectMeeting' => function ($query) {

                    $query->with([
                        'adminUser' => function ($query) {
                            $query->field('id,username,nickname,phone')->where('status', 1);
                        },
                        // 项目会议 一对多 项目会议邀请讲者
                        'projectMeetingInvitationSpeaker' => function ($query) {
                            // field(
                            //     '
                            //         id,
                            //         project_id,
                            //         project_meeting_id,
                            //         speaker_id,
                            //         speaker_name,
                            //         speaker_email,
                            //         meeting_type,
                            //         meeting_remarks,
                            //         invitation_status,
                            //         is_display,
                            //         created_by
                            //         '
                            // )->
                            $query->with(array_merge([
                                'emailRecord' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                                // 关联资料上传表
                                // => function ($query) {
                                //     $query->field(' id,
                                //                         invitation_speaker_id,
                                //                         meeting_time_bj,
                                //                         meeting_time_en,
                                //                         meeting_start_time,
                                //                         meeting_end_time,
                                //                         service_charge,
                                //                         payer,
                                //                         meeting_info_is_display,
                                //                         meeting_materiel,
                                //                         meeting_screen_recording,
                                //                         meeting_photo,
                                //                         meeting_materiel_is_display,
                                //                         meeting_ppt,
                                //                         meeting_ppt_is_display');
                                // }
                                'uploadData' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // cuc账户录入
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                // }
                                'cuc' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 劳务费支付
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                // }
                                'charge' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // @todo  如果外讲库的讲者简历、护照、付款信息有的话用那俩表的eb_speaker_passport、eb_speaker_resume
                                // 索要讲者简历+照片
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,speaker_resume,speaker_resume_translate,speaker_avatar');
                                // }
                                'speakerResume' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // @todo  更新 eb_speaker_resume表付款信息
                                // 索要讲者付款信息+护照 
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,remittance,surname,name,birth,passport_number,nationality,sex,place_of_birth,passport_issuing_place,validity,upload_passport');
                                // }
                                'speakerPassport' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 签订协议
                                // => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,sign_agreement_url');
                                // }
                                'signAgreement' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // PPT要求及返回截止日期
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_return_deadline');
                                // }
                                'pptReturnDeadline' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // PPT跟进
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_follow_up');
                                // }
                                'pptFollowUp' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 翻译PPT给到讲者
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate');
                                // }
                                'pptTranslate' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 翻译讨论问题给讲者
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate_problem');
                                // }
                                'translateToLecturer' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                'speaker' => function ($query) {
                                    $query->with([
                                        'resume',
                                        'passport'
                                    ])
                                        ->where('status', 1);
                                    //->field('id,institution,institution_en')
                                },

                            ], [
                                // 线上会议信息+测试需求给到讲者
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,conference_number,password,conference_url,conference_is_display,is_test,test_time,test_is_display');
                                // }
                                'meetingOnline' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 可编辑日程翻译给讲者
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,schedule_url');
                                // }
                                'meetingSchedule' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 会后感谢信
                                // => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                // }
                                'meetingAfter' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                            ], [
                                // 签证计划
                                // => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                // }
                                'visaProgram' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 签证包发送
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,visa_url');
                                // }
                                'visaPackage' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 邀请函
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_url');
                                // }
                                'invitationLetter' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 邀请函邮寄发单号
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_number');
                                // }
                                'invitationLetterNumber' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // @todo  更新外讲表  eb_speaker_resume  的常旅客号
                                // 索要常旅客号
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,frequent_flyer_number');
                                // }
                                'frequentFlyer' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 发讲者喜好问卷
                                //  =>  function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,questionnaire_url');
                                // }
                                'meetingQuestions' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 行程终版+国际机票+酒店信息给讲者
                                //   => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,final_travel,ticket,hotel');
                                // }
                                'finalTravel' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 索要中国签证扫描页
                                //   => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,scan_url');
                                // }
                                'chinaVsiaScan' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 接机安排和温馨提示等
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                                // }
                                'pickUpAndTips' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 活动感谢信+询问报销+出入境章扫描件
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,thank_you,reimbursement,scan');
                                // }
                                'scan' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 索要报销单据扫描件
                                //   => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,reimbursement_url');
                                // }
                                'reimbursement' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                                // 发结算单
                                //  => function ($query) {
                                //     $query->field('id,invitation_speaker_id,status,remarks,is_display,final_statement_url');
                                // }
                                'statement' => function ($query) {
                                    $query->with([
                                        'adminUser' => function ($query) {
                                            $query->field('id,username,nickname,phone')->where('status', 1);
                                        },
                                    ]);
                                },
                            ]));
                        }
                    ]);
                    // ->field('id,project_id,meeting_name,status,confirm_invitation,total_invitation,stop_remarks,stop_time,is_display,create_time,update_time,created_by,updated_by')
                }
            ]
        )
            ->where('id', $projectId)
            // ->field('id,project_name')
            ->find();

        // $res = customHelpers::getUrl($projectMeeting->toArray());
        return $this->success($projectMeeting);
        // var_dump($projectMeeting->toArray());
        // 数据，可以直接传数组，无需序列化
        // 查询文件地址
        // $data = ['to' => 'tom@gmail.com', 'content' => 'hello'];
        // if ($res) {
        //     // 投递消息
        //     Client::send($queue, $res);

        //     return $this->success([], '文件正在生成中···');
        // }
    }

    /**
     * 项目会议数据导出
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-11
     * @param Request $request
     * @return void
     */
    public function projectMeetingExport(Request $request)
    {
        $projectMeetingId = $request->input('project_meeting_id');

        $projectMeeting = ProjectMeeting::with(
            [
                'project' => function ($query) {
                    $query->with([
                        'usersOne' => function ($query) {
                            $query->field('id,username,realname,avatar,email,company_name,phone,last_login_time');
                        },
                        'users',
                    ]);
                },

                // 项目会议 一对多 项目会议邀请讲者
                'projectMeetingInvitationSpeaker' => function ($query) {
                    // field(
                    //     '
                    //         id,
                    //         project_id,
                    //         project_meeting_id,
                    //         speaker_id,
                    //         speaker_name,
                    //         speaker_email,
                    //         meeting_type,
                    //         meeting_remarks,
                    //         invitation_status,
                    //         is_display,
                    //         created_by
                    //         '
                    // )->
                    $query->with(array_merge([
                        'emailRecord' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        'adminUser' => function ($query) {
                            $query->field('id,username,nickname,phone')->where('status', 1);
                        },
                        // 关联资料上传表
                        // => function ($query) {
                        //     $query->field(' id,
                        //                         invitation_speaker_id,
                        //                         meeting_time_bj,
                        //                         meeting_time_en,
                        //                         meeting_start_time,
                        //                         meeting_end_time,
                        //                         service_charge,
                        //                         payer,
                        //                         meeting_info_is_display,
                        //                         meeting_materiel,
                        //                         meeting_screen_recording,
                        //                         meeting_photo,
                        //                         meeting_materiel_is_display,
                        //                         meeting_ppt,
                        //                         meeting_ppt_is_display');
                        // }
                        'uploadData' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // cuc账户录入
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'cuc' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 劳务费支付
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'charge' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // @todo  如果外讲库的讲者简历、护照、付款信息有的话用那俩表的eb_speaker_passport、eb_speaker_resume
                        // 索要讲者简历+照片
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,speaker_resume,speaker_resume_translate,speaker_avatar');
                        // }
                        'speakerResume' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // @todo  更新 eb_speaker_resume表付款信息
                        // 索要讲者付款信息+护照 
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,remittance,surname,name,birth,passport_number,nationality,sex,place_of_birth,passport_issuing_place,validity,upload_passport');
                        // }
                        'speakerPassport' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 签订协议
                        // => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,sign_agreement_url');
                        // }
                        'signAgreement' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // PPT要求及返回截止日期
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_return_deadline');
                        // }
                        'pptReturnDeadline' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // PPT跟进
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_follow_up');
                        // }
                        'pptFollowUp' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 翻译PPT给到讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate');
                        // }
                        'pptTranslate' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 翻译讨论问题给讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate_problem');
                        // }
                        'translateToLecturer' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        'speaker' => function ($query) {
                            $query->with([
                                'resume',
                                'passport'
                            ])
                                ->where('status', 1);
                            //->field('id,institution,institution_en')
                        },

                    ], [
                        // 线上会议信息+测试需求给到讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,conference_number,password,conference_url,conference_is_display,is_test,test_time,test_is_display');
                        // }
                        'meetingOnline' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 可编辑日程翻译给讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,schedule_url');
                        // }
                        'meetingSchedule' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 会后感谢信
                        // => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'meetingAfter' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                    ], [
                        // 签证计划
                        // => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'visaProgram' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 签证包发送
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,visa_url');
                        // }
                        'visaPackage' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 邀请函
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_url');
                        // }
                        'invitationLetter' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 邀请函邮寄发单号
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_number');
                        // }
                        'invitationLetterNumber' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // @todo  更新外讲表  eb_speaker_resume  的常旅客号
                        // 索要常旅客号
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,frequent_flyer_number');
                        // }
                        'frequentFlyer' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 发讲者喜好问卷
                        //  =>  function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,questionnaire_url');
                        // }
                        'meetingQuestions' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 行程终版+国际机票+酒店信息给讲者
                        //   => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,final_travel,ticket,hotel');
                        // }
                        'finalTravel' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 索要中国签证扫描页
                        //   => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,scan_url');
                        // }
                        'chinaVsiaScan' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 接机安排和温馨提示等
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'pickUpAndTips' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 活动感谢信+询问报销+出入境章扫描件
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,thank_you,reimbursement,scan');
                        // }
                        'scan' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 索要报销单据扫描件
                        //   => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,reimbursement_url');
                        // }
                        'reimbursement' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 发结算单
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,final_statement_url');
                        // }
                        'statement' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                    ]));
                }
            ]

        )
            ->whereIn('id', $projectMeetingId)
            // ->field('id,project_name')
            ->select();

        // $res = customHelpers::getUrl($projectMeeting->toArray());
        return $this->success($projectMeeting);
    }


    public function projectMeetingSpeakerExport(Request $request)
    {
        // 单个项目id
        $projectMeetingId = $request->input('project_meeting_id');
        // 单个或多个讲者id
        $projectMeetingSpeakerId = $request->input('speaker_id');

        $projectMeeting = ProjectMeeting::with(
            [
                'project' => function ($query) {
                    $query->with([
                        'usersOne' => function ($query) {
                            $query->field('id,username,realname,avatar,email,company_name,phone,last_login_time');
                        },
                        'users',
                    ]);
                },

                // 项目会议 一对多 项目会议邀请讲者
                'projectMeetingInvitationSpeaker' => function ($query) use ($projectMeetingSpeakerId) {
                    // field(
                    //     '
                    //         id,
                    //         project_id,
                    //         project_meeting_id,
                    //         speaker_id,
                    //         speaker_name,
                    //         speaker_email,
                    //         meeting_type,
                    //         meeting_remarks,
                    //         invitation_status,
                    //         is_display,
                    //         created_by
                    //         '
                    // )->
                    $query->with(array_merge([
                        'emailRecord' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        'adminUser' => function ($query) {
                            $query->field('id,username,nickname,phone')->where('status', 1);
                        },
                        // 关联资料上传表
                        // => function ($query) {
                        //     $query->field(' id,
                        //                         invitation_speaker_id,
                        //                         meeting_time_bj,
                        //                         meeting_time_en,
                        //                         meeting_start_time,
                        //                         meeting_end_time,
                        //                         service_charge,
                        //                         payer,
                        //                         meeting_info_is_display,
                        //                         meeting_materiel,
                        //                         meeting_screen_recording,
                        //                         meeting_photo,
                        //                         meeting_materiel_is_display,
                        //                         meeting_ppt,
                        //                         meeting_ppt_is_display');
                        // }
                        'uploadData' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // cuc账户录入
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'cuc' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 劳务费支付
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'charge' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // @todo  如果外讲库的讲者简历、护照、付款信息有的话用那俩表的eb_speaker_passport、eb_speaker_resume
                        // 索要讲者简历+照片
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,speaker_resume,speaker_resume_translate,speaker_avatar');
                        // }
                        'speakerResume' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // @todo  更新 eb_speaker_resume表付款信息
                        // 索要讲者付款信息+护照 
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,remittance,surname,name,birth,passport_number,nationality,sex,place_of_birth,passport_issuing_place,validity,upload_passport');
                        // }
                        'speakerPassport' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 签订协议
                        // => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,sign_agreement_url');
                        // }
                        'signAgreement' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // PPT要求及返回截止日期
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_return_deadline');
                        // }
                        'pptReturnDeadline' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // PPT跟进
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_follow_up');
                        // }
                        'pptFollowUp' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 翻译PPT给到讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate');
                        // }
                        'pptTranslate' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 翻译讨论问题给讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,ppt_translate_problem');
                        // }
                        'translateToLecturer' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        'speaker' => function ($query) {
                            $query->with([
                                'resume',
                                'passport'
                            ])
                                ->where('status', 1);
                            //->field('id,institution,institution_en')
                        },

                    ], [
                        // 线上会议信息+测试需求给到讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,conference_number,password,conference_url,conference_is_display,is_test,test_time,test_is_display');
                        // }
                        'meetingOnline' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 可编辑日程翻译给讲者
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,schedule_url');
                        // }
                        'meetingSchedule' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 会后感谢信
                        // => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'meetingAfter' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                    ], [
                        // 签证计划
                        // => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'visaProgram' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 签证包发送
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,visa_url');
                        // }
                        'visaPackage' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 邀请函
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_url');
                        // }
                        'invitationLetter' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 邀请函邮寄发单号
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,letter_number');
                        // }
                        'invitationLetterNumber' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // @todo  更新外讲表  eb_speaker_resume  的常旅客号
                        // 索要常旅客号
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,frequent_flyer_number');
                        // }
                        'frequentFlyer' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 发讲者喜好问卷
                        //  =>  function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,file_name,questionnaire_url');
                        // }
                        'meetingQuestions' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 行程终版+国际机票+酒店信息给讲者
                        //   => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,final_travel,ticket,hotel');
                        // }
                        'finalTravel' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 索要中国签证扫描页
                        //   => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,scan_url');
                        // }
                        'chinaVsiaScan' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 接机安排和温馨提示等
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display');
                        // }
                        'pickUpAndTips' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 活动感谢信+询问报销+出入境章扫描件
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,thank_you,reimbursement,scan');
                        // }
                        'scan' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 索要报销单据扫描件
                        //   => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,reimbursement_url');
                        // }
                        'reimbursement' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                        // 发结算单
                        //  => function ($query) {
                        //     $query->field('id,invitation_speaker_id,status,remarks,is_display,final_statement_url');
                        // }
                        'statement' => function ($query) {
                            $query->with([
                                'adminUser' => function ($query) {
                                    $query->field('id,username,nickname,phone')->where('status', 1);
                                },
                            ]);
                        },
                    ]))->whereIn('id', $projectMeetingSpeakerId);
                }
            ]

        )
            ->where('id', $projectMeetingId)
            // ->field('id,project_name')
            ->select();

        // $res = customHelpers::getUrl($projectMeeting->toArray());
        return $this->success($projectMeeting);
    }

    /**
     * 创建新用户
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-19
     * @param Request $request
     * @return void
     */
    public function createUser(Request $request)
    {
        $payload = $request->more([
            ['username', ''],
            ['realname', ''],
            ['email', ''],
            ['password', ''],
            ['company_name', ''],
            ['status', 1],

        ]);

        $validate = new UsersValidate();
        if (!$validate->check($payload)) {
            return $this->fail($validate->getError());
        }


        $user = Users::where('username', $payload['username'])->findOrEmpty();
        if (!$user->isEmpty()) {
            return $this->fail('账号已经存在了');
        }

        $payload['phone'] = $payload['username'];

        $result = Users::create($payload);

        if ($result) {
            return $this->success(['id' => $result->id, 'username' => $result->username, 'realname' => $result->realname, '']);
        } else {
            return $this->fail('添加失败');
        }
    }

    /**
     * 创建项目
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2025-01-17
     * @param Request $request
     * @return void
     */
    public function save(Request $request): Response
    {
        $data = $request->post();

        if ($this->validate) {
            if (!$this->validate->scene('save')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
        $result = $this->logic->save($data);
        if ($result) {
            if (!empty($data['user_id'])) {
                $res = $this->logic->order('id', 'desc')->find();
                $this->customAfterChange('save', $res->id, $data['user_id']);
            }
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 修改项目
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2025-01-17
     * @param Request $request
     * @param [type] $id
     * @return void
     */
    public function update(Request $request, $id): Response
    {
        // 项目id
        $id = $request->input('id', $id);

        $data = $request->post();
        if ($this->validate) {
            if (!$this->validate->scene('update')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }

        $info = $this->logic->find($id);

        if (!$info) {

            return $this->fail('没有找到该数据');
        }
        $result = $this->logic->update($data, [$this->pk => $id]);
        if ($result) {
            if (!empty($data['user_id'])) {
                $this->customAfterChange('update', $id, $data['user_id']);
            }
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 添加、修改成功之后后续操作
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2025-01-17
     * @param [type] $type
     * @param [type] $projectId
     * @param [type] $userId
     * @return void
     */
    public function customAfterChange($type, $projectId, $userId)
    {
        $project = Project::where('id', $projectId)->find();
        $userId = explode(',', $userId);
        if ($type == 'save') {
            $project->usersProject()->saveAll($userId);
        } elseif ($type == 'update') {
            // 先删除关系
            $project->usersProject()->detach();
            // 再关联新关系
            $project->usersProject()->saveAll($userId);
        }
    }


    /**
     * 读取数据
     * @param Request $request
     * @param $id
     * @return Response
     */
    public function read(Request $request, $id): Response
    {
        $id = $request->input('id', $id);
        $model = $this->logic->with(
            [
                'users' => function ($query) {
                    $query->field('username,realname');
                }
            ]
        )->find($id);
        if ($model) {
            $data = is_array($model) ? $model : $model->toArray();
            return $this->success($data);
        } else {
            return $this->fail('未查找到信息');
        }
    }
}
